#pragma once
class Game;  // 前向声明

#include <SFML/Graphics.hpp>
#include <memory>
#include <vector>
#include <cmath>
#include "Bullet.hpp"
#include "Map.hpp"

class Tank {
public:
    enum class Type {
        Player,
        Enemy
    };
    
    enum class Direction {
        Up,
        Right,
        Down,
        Left
    };
    
    Tank(Type type, const sf::Vector2f& position, Game& game);
    
    void handleInput(bool isMovingUp, bool isMovingDown,
                    bool isMovingLeft, bool isMovingRight,
                    bool isShooting);
    void update(sf::Time deltaTime);
    void render(sf::RenderWindow& window);
    
    void move(const sf::Vector2f& offset);
    void setDirection(Direction dir);  // 新增：设置方向
    void shoot();
    void damage(int amount);
    void undoLastMove();
    
    sf::FloatRect getBounds() const;
    int getHealth() const { return mHealth; }
    bool isExploding() const { return mIsExploding; }
    bool isExploded() const { return mIsExploded; }
    Type getType() const { return mType; }
    
    bool checkBulletCollisions(Map& map);
    bool checkBulletCollisions(Tank& other);
    bool checkBulletCollisionsWithBullets(Tank& other);  // 新增：检查子弹之间的碰撞
    
    static void loadTextures();

private:
    void createSprite();
    void updateBullets(sf::Time deltaTime);
    
    static sf::Texture playerTexture;
    static sf::Texture enemyTexture;
    static sf::Texture explosionTexture;
    static bool texturesLoaded;
    
    Game& mGame;  // 游戏引用
    Type mType;
    Direction mCurrentDirection;  // 新增：当前方向
    sf::Vector2f mPosition;
    sf::Vector2f mLastPosition;
    float mRotation;
    float mSpeed = 100.f;
    int mHealth = 100;
    
    sf::Sprite mSprite;
    std::vector<std::unique_ptr<Bullet>> mBullets;
    
    sf::Clock mShootingClock;  // 射击计时器
    sf::Clock mExplosionClock; // 爆炸动画计时器
    bool mIsExploding = false; // 是否正在爆炸
    bool mIsExploded = false;  // 是否已经爆炸完成
    bool mIsSpawning = false;  // 是否正在出现
    float mSpawnAlpha = 0.f;   // 出现动画的透明度
    
    static constexpr float SHOT_INTERVAL = 0.5f;
    static constexpr float EXPLOSION_DURATION = 0.5f; // 爆炸动画持续时间
    static constexpr float SPAWN_DURATION = 0.5f;     // 出现动画持续时间
    static constexpr float TANK_WIDTH = 64.f;
    static constexpr float TANK_HEIGHT = 64.f;
    
public:
    // 获取位置
    const sf::Vector2f& getPosition() const { return mPosition; }
    
    // 设置玩家位置（用于AI）
    void setPlayerPosition(const sf::Vector2f& pos) { mPlayerPosition = pos; }

    // AI相关
    sf::Vector2f mTargetPosition;  // AI的目标位置
    bool mHasTarget = false;       // 是否有目标位置
    static constexpr float MAP_WIDTH = 800.f;   // 地图宽度
    static constexpr float MAP_HEIGHT = 600.f;  // 地图高度
    static constexpr float GRID_SIZE = 32.f;    // 网格大小
    
    // 将位置对齐到网格
    void alignToGrid() {
        mPosition.x = std::round(mPosition.x / GRID_SIZE) * GRID_SIZE;
        mPosition.y = std::round(mPosition.y / GRID_SIZE) * GRID_SIZE;
        mSprite.setPosition(mPosition);
    }
    
private:
    sf::Vector2f mPlayerPosition;  // 玩家位置（用于AI）
};